package com.yizhi.service.Impl;

import com.yizhi.domain.entityDO.UserDO;
import com.yizhi.exception.PasswordErrorException;
import com.yizhi.mapper.UserMapper;
import com.yizhi.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.AccountNotFoundException;

/**
 * @author zhouyihe
 * @ClassName LoginServiceImpl
 * @qq 1552951165
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public UserDO userLogin(String username, String password) throws AccountNotFoundException {
        UserDO user = userMapper.selectByUsername(username);
        
        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            //账号不存在
            throw new AccountNotFoundException("账号不存在");
        }
        
        //密码比对
        //对前端传递过来的明文密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPwd())) {
            //密码错误
            throw new PasswordErrorException("密码错误");
        }
        
        //3、返回实体对象
        return user;
    }
}
